#include "StdAfx.h"
#include "RGBColour.h"
#include "Math.h"

RGBColour::RGBColour(void): r(0.0), g(0.0), b(0.0)
{
}

RGBColour::RGBColour(float _r, float _g, float _b): r(_r), g(_g), b(_b)
{
}

RGBColour::~RGBColour(void)
{
}

RGBColour RGBColour::rgb2hsv(RGBColour& col) {
	float minRGB;
	float maxRGB;
	float computedH = 0;
	float computedS = 0;
	float computedV = 0;

	float r = col.r/255; 
	float g = col.g/255; 
	float b = col.b/255;

	if(r<g)
	{
		if(r<b)
		{minRGB = r;}
		else
		{minRGB = b;}
	}
	else if (g<b)
	{minRGB = g;}
	else
	{minRGB = b;}
	if(r>g)
	{
		if(r>b)
		{maxRGB = r;}
		else
		{maxRGB = b;}
	}
	else if (g>b)
	{maxRGB = g;}
	else
	{maxRGB = b;}

	// Black-gray-white
	if (minRGB==maxRGB) 
	{
		computedV = minRGB;
		return RGBColour(0,0,computedV);
	}

	// Colors other than black-gray-white:
	float d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
	float h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5);
	computedH = 60*(h - d/(maxRGB - minRGB));
	computedS = (maxRGB - minRGB)/maxRGB;
	computedV = maxRGB;
	return RGBColour(computedH,computedS,computedV);
}
